/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';
import * as types from './actionTypes';
import { DOMParser } from 'xmldom';
import XmlHelper from '../utils/xmlhelper'
import {GetNewsList,GetNewsShow,GetNewsDetail} from '../services/newsServices';

function requestNewsList()
{
    return {
        type: types.REQUEST_NEWS_LIST,
    };
}

function requestNewsListHot()
{
    return {
        type: types.REQUEST_NEWS_LIST_HOT,
    };
}

function requestNewsDetail()
{
    return {
        type: types.REQUEST_NEWS_DETAIL,
    };
}

function getNewsListSource(data) {
    return {
        type: types.RECEIVE_NEWS_LIST,
        data
    };
}

function getNewsListHotSource(show,data) {
    return {
        type: types.RECEIVE_NEWS_LIST_HOT,
        data
    };
}

function getNewsDetailSource(data) {
    return {
        type: types.RECEIVE_NEWS_DETAIL,
        data
    };
}

function readNewsListToJson(xml) {
    const parser = new DOMParser();
    const xmlDoc = parser.parseFromString(xml);
    var jsonObj=null;
    try {
        var jsonStr= XmlHelper.xml2json(xmlDoc,'');
        jsonObj= JSON.parse(jsonStr);
    } catch (error) {
        return {result:0,msg:'解析成对象失败',newsList:[]};
    }
    if(jsonObj&&jsonObj.oschina&&jsonObj.oschina.newslist&&jsonObj.oschina.newslist.news)
    {
        return {result:1,msg:'获取完毕',newsList:jsonObj.oschina.newslist.news};
    }
    else{
        return {result:0,msg:'请求返回异常',newsList:[]};
    }
}

export function GetNewsListSource(catalog,pageIndex,pageSize)
{
    return dispatch => {
        dispatch(requestNewsList());
        return GetNewsList(catalog,pageIndex,pageSize).then(response => response.text())
        .then(text =>
        {
            var newJson = readNewsListToJson(text);
            dispatch(getNewsListSource(newJson.newsList));
        }).catch((error)=>{dispatch(getNewsListSource([]))});
    };
}

export function GetNewsShowSource(show)
{
    return dispatch => {
        dispatch(requestNewsListHot());
        return GetNewsList(catalog,pageIndex,pageSize).then(response => response.text())
        .then(text =>
        {
            var newJson = readNewsListToJson(text);
            dispatch(getNewsListHotSource(show,newJson.newsList));
        }).catch((error)=>{dispatch(getNewsListHotSource(show,[]))});
    };
}

function readNewsDetailToJson(xml) {
    const parser = new DOMParser();
    const xmlDoc = parser.parseFromString(xml);
    var jsonObj=null;
    try {
        var jsonStr= XmlHelper.xml2json(xmlDoc,'');
        jsonObj= JSON.parse(jsonStr);
    } catch (error) {
        return {result:0,msg:'解析成对象失败',news:[]};
    }
    if(jsonObj&&jsonObj.oschina&&jsonObj.oschina.news)
    {
        return {result:1,msg:'获取完毕',news:jsonObj.oschina.news};
    }
    else{
        return {result:0,msg:'请求返回异常',news:[]};
    }
}

export function GetNewsDetailSource(id)
{
    return dispatch => {
        dispatch(requestNewsDetail());
        return GetNewsDetail(id).then(response => response.text())
        .then(text =>
        {
            var newJson = readNewsDetailToJson(text);
            dispatch(getNewsDetailSource(newJson.news));
        }).catch((error)=>{dispatch(getNewsDetailSource([]))});
    };
}


